unit GMedUzsakymaiIrasai;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GFixForm, bendri, Sandelys_dm, StdCtrls, Mask,
  DBCtrls, Buttons, ActnList,
  ExtCtrls, cxLookAndFeelPainters, cxButtons, cxTextEdit,
  cxDBEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, cxControls, cxContainer, cxEdit, cxMaskEdit, cxCalc,
  DB, cxPropertiesStore, Menus, cxGraphics;

type
  TfMedUzsakymaiIrasai = class(TGamaFixForm)
    sMode: TStaticText;
    ActionList1: TActionList;
    aPaimtiPreke: TAction;
    pOK: TPanel;
    bCancel: TcxButton;
    bOK: TcxButton;
    DS: TDataSource;
    GamaPropertiesStore: TcxPropertiesStore;
    ePlotas: TcxDBCalcEdit;
    Label2: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    eRusis: TcxDBLookupComboBox;
    Label1: TLabel;
    eStoris: TcxDBLookupComboBox;
    ePlotis: TcxDBLookupComboBox;
    eIlgis: TcxDBLookupComboBox;
    Label3: TLabel;
    eKaina: TcxDBCalcEdit;
    Label4: TLabel;
    ePVM: TcxDBCalcEdit;
    eSuma: TcxTextEdit;
    ePVMSuma: TcxTextEdit;
    Label7: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    eSumaSuPVM: TcxTextEdit;
    ePTipas: TcxDBLookupComboBox;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure aPaimtiPrekeExecute(Sender: TObject);
    procedure pBSkaneriaiEnter(Sender: TObject);
    procedure pBSkaneriaiExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure eKiekisPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption;
      var Error: Boolean);
    procedure KainaPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption;
      var Error: Boolean);
  private
    { Private declarations }
    PVM: double;
    procedure SetMode(v: string);
    procedure PriimtiPreke(Sender: TObject);
    procedure ePTipasChange(Sender: TObject);
    procedure eValKainaChange(Sender: TObject);
  public
    { Public declarations }
    procedure ClearMode;
    property Mode: string write SetMode;
  end;

var
  fMedUzsakymaiIrasai: TfMedUzsakymaiIrasai;

implementation

uses
  DBClient, Math, sandelys_conf, doktip,  uSandDokTip, med_dm,
  tools, JvJCLUtils;

{$R *.dfm}

procedure TfMedUzsakymaiIrasai.FormShow(Sender: TObject);
begin

  DS.Enabled := true;

  ePTipas.Properties.OnEditValueChanged := ePTipasChange;

  eKaina.Properties.OnEditValueChanged := eValKainaChange;
  ePVM.Properties.OnEditValueChanged := eValKainaChange;
  ePlotas.Properties.OnEditValueChanged := eValKainaChange;

  ActiveControl := eStoris;
end;

procedure TfMedUzsakymaiIrasai.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin

  ePTipas.Properties.OnEditValueChanged := nil;
  eStoris.Properties.OnEditValueChanged := nil;
  ePlotis.Properties.OnEditValueChanged := nil;
  eIlgis.Properties.OnEditValueChanged := nil;

  eKaina.Properties.OnEditValueChanged := nil;
  ePVM.Properties.OnEditValueChanged := nil;
  ePlotas.Properties.OnEditValueChanged := nil;

  DS.Enabled := false;
end;

procedure TfMedUzsakymaiIrasai.ePTipasChange(Sender: TObject);
var
  PrekesID: integer;
  SandelioID: integer;
  fLikutis: double;
  PrPsl: string;
  tbSQL: TClientDataSet;
  tbSQLMedSN: TClientDataSet;
  pTipas: string;
begin
  pTipas := VarToStr(ePTipas.EditValue);

  if pTipas[1] = 'P' then begin
    eStoris.RepositoryItem := dmMed.cxEditRepositoryLookupParkStoris;
    ePlotis.RepositoryItem := dmMed.cxEditRepositoryLookupParkPlotis;
    eRusis.RepositoryItem  := dmMed.cxEditRepositoryLookupParkRusis;
    eIlgis.RepositoryItem  := dmMed.cxEditRepositoryLookupMedIlgis;
  end;
  if pTipas[1] = 'G' then begin
    eStoris.RepositoryItem := dmMed.cxEditRepositoryLookupParkStoris;
    ePlotis.RepositoryItem := dmMed.cxEditRepositoryLookupParkPlotis;
    eRusis.RepositoryItem  := dmMed.cxEditRepositoryLookupParkRusis;
    eIlgis.RepositoryItem  := dmMed.cxEditRepositoryLookupMedIlgis;
  end;
  if pTipas[1] = 'M' then begin
    eStoris.RepositoryItem := dmMed.cxEditRepositoryLookupMedStoris;
    ePlotis.RepositoryItem := dmMed.cxEditRepositoryLookupMedPlotis;
    eIlgis.RepositoryItem  := dmMed.cxEditRepositoryLookupMedIlgis;
    eRusis.RepositoryItem  := dmMed.cxEditRepositoryLookupMedRusis;
  end;
  if pTipas[1] = 'R' then begin
    eStoris.RepositoryItem := dmMed.cxEditRepositoryLookupRastStoris;
    eIlgis.RepositoryItem := dmMed.cxEditRepositoryLookupRastIlgis;
    eRusis.RepositoryItem  := dmMed.cxEditRepositoryLookupMedRusis;
  end;
  
end;

procedure TfMedUzsakymaiIrasai.SetMode(v: string);
begin
  sMode.Caption := v;
end;

procedure TfMedUzsakymaiIrasai.ClearMode;
begin
  sMode.Caption := '';
end;

procedure TfMedUzsakymaiIrasai.aPaimtiPrekeExecute(Sender: TObject);
begin
  RegDokTipas[dtPrekes].Forma.Paimti(self.PriimtiPreke);
end;

procedure TfMedUzsakymaiIrasai.PriimtiPreke(Sender: TObject);
var
  SN: string;
  AfterInsert: TDataSetNotifyEvent;
begin
  with dmSandelys do begin
    if not (DS.DataSet.State in [dsInsert, dsEdit]) then Exit;
    RegDokTipas[dtPrekes].Forma.RefreshTables;
    DS.DataSet['PrekesID'] := tbPrekes['ID'];

    // yra duotas ir serijinis numeris
    SN := RegDokTipas[dtPrekes].Forma.ExValue[3];
    if Trim(SN) <> '' then
      DS.DataSet['SerNr'] := Trim(SN);
  end; // with dmSandelys do begin
  RegDokTipas[dtPrekes].Forma.Paimti(nil);
  ActiveControl := ePlotas;
end;

// paskaiciuojame suma
procedure TfMedUzsakymaiIrasai.eValKainaChange(Sender: TObject);
var
  fSuma: Extended;
begin
  PVM := ePVM.Value;
  fSuma := eKaina.Value * ePlotas.Value;
  eSuma.Text := FormatFloat( dmSandelys.SandConfigParam.KainaDisplay, fSuma ) + ' ';

  //fSuma := eKaina.Value * ePlotas.Value * (1.0 - eNuolaida.Value/100.0);
  //eSumaSuNuolaida.Text := FormatFloat( dmSandelys.SandConfigParam.KainaDisplay, fSuma ) + ' ';
  if PVM > 0 then begin
    ePVMSuma.Text := FormatFloat( dmSandelys.SandConfigParam.KainaDisplay, fSuma * PVM  / 100.0 ) + ' ';
    eSumaSuPVM.Text := FormatFloat( dmSandelys.SandConfigParam.KainaDisplay, fSuma * (1.0 + PVM / 100.0) ) + ' ';
  end else begin
    ePVMSuma.Text := FormatFloat( dmSandelys.SandConfigParam.KainaDisplay, fSuma ) + ' ';
    eSumaSuPVM.Text := FormatFloat( dmSandelys.SandConfigParam.KainaDisplay, fSuma ) + ' ';
  end;
end;

procedure TfMedUzsakymaiIrasai.pBSkaneriaiEnter(Sender: TObject);
begin
  KeyPreview := false;
end;

procedure TfMedUzsakymaiIrasai.pBSkaneriaiExit(Sender: TObject);
begin
  KeyPreview := true;
end;

procedure TfMedUzsakymaiIrasai.FormCreate(Sender: TObject);
begin
  DS.Enabled := false;
end;

procedure TfMedUzsakymaiIrasai.eKiekisPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  if DisplayValue <> '' then
  DisplayValue := Rounding(DisplayValue, dmSandelys.SandConfigParam.KiekioTikslumas);
end;

procedure TfMedUzsakymaiIrasai.KainaPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  if DisplayValue <> '' then
  DisplayValue := Rounding(DisplayValue, dmSandelys.SandConfigParam.KainosTikslumas);
end;

end.
